package com.lsy.weizhi2.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lsy.weizhi2.JsonObject.ActivityListObject;
import com.lsy.weizhi2.JsonObject.ReplyList;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.customview.ActivityDetailUp;
import com.lsy.weizhi2.customview.ReplyDailog;
import com.lsy.weizhi2.customview.ReplyUserItem;
import com.lsy.weizhi2.myutils.DetailAdapterCall;
import com.lsy.weizhi2.myutils.NoteDetailUPCall;
import com.lsy.weizhi2.myutils.ReplyDailogCall;
import com.lsy.weizhi2.myutils.ReplyUserItemCall;
import com.lsy.weizhi2.utils.MyOkHttpUtils;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 嗨哟 on 2017/5/10.
 */

public class ActivityDetailAdapter2 extends RecyclerView.Adapter {
    private Context context;
    private ActivityListObject.ListBean noteDetailBean;
    private LayoutInflater layoutInflater;
    private DetailAdapterCall detailAdapterCall;
    private List<ReplyList.ListBean> list_r;
    private int count,tag;
    private final int content=1;
    private final int content_2=4;
    private final int foote=2;
    public ActivityDetailAdapter2(Context context, ActivityListObject.ListBean noteDetailBean) {
        this.context = context;
        this.noteDetailBean = noteDetailBean;
        layoutInflater=LayoutInflater.from(context);
    }

    public void setDetailAdapterCall(DetailAdapterCall detailAdapterCall){
        this.detailAdapterCall=detailAdapterCall;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View replyview= layoutInflater.inflate(R.layout.reply_item,parent,false);
        View replyview2 = layoutInflater.inflate(R.layout.reply_reply_item,parent,false);
        View bottomview= layoutInflater.inflate(R.layout.bottom_view,parent,false);
        AutoUtils.autoSize(replyview);
        AutoUtils.autoSize(replyview2);
        AutoUtils.autoSize(bottomview);
        if(viewType==foote){
            return new Bottom(bottomview);
        }else if(viewType==content){
            return new ListViewHolder(replyview);
        }else if(viewType==content_2){
            return new Reply_Reply(replyview2);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
       if(holder instanceof ActivityDetailAdapter.ListViewHolder){
            ((ActivityDetailAdapter.ListViewHolder) holder).position=position;
            if(list_r!=null&&list_r.size()!=0){
                ((ActivityDetailAdapter.ListViewHolder) holder).reply_floor.setText((position)+"楼");
                ((ActivityDetailAdapter.ListViewHolder) holder).reply_user.setInfo(list_r.get(position-1));
                ((ActivityDetailAdapter.ListViewHolder) holder).reply_content.setText(list_r.get(position-1).getContent());
                ((ActivityDetailAdapter.ListViewHolder) holder).yinyong.setVisibility(View.GONE);
            }
        }else if(holder instanceof ActivityDetailAdapter.Reply_Reply){
            ((ActivityDetailAdapter.Reply_Reply) holder).position=position;
            if(list_r!=null&&list_r.size()!=0){
                ((ActivityDetailAdapter.Reply_Reply) holder).replyUserItem.setInfo(list_r.get(position-1));
                String content=list_r.get(position-1).getContent();
                String[] str=content.split("&");
                if(str.length==1){
                    ((ActivityDetailAdapter.Reply_Reply) holder).reply_content.setText("");
                    ((ActivityDetailAdapter.Reply_Reply) holder).reply_reply_content.setText(str[0]);
                }else{
                    ((ActivityDetailAdapter.Reply_Reply) holder).reply_content.setText(str[0]);
                    ((ActivityDetailAdapter.Reply_Reply) holder).reply_reply_content.setText(str[1]);
                }
            }
        }else if(holder instanceof ActivityDetailAdapter.Bottom){
            if(tag==2){
                ((ActivityDetailAdapter.Bottom) holder).bottom_tv.setText("松开加载");
            }else if(tag==1){
                ((ActivityDetailAdapter.Bottom) holder).bottom_tv.setText("正在加载");
            }else if(tag==3){
                ((ActivityDetailAdapter.Bottom) holder).bottom_tv.setText("到底了");
            }
        }
    }

    @Override
    public int getItemCount() {
        return list_r==null?count:list_r.size()+count;
    }
    public void UpData(List<ReplyList.ListBean>list_r,int count,int tag){
        this.list_r=list_r;
        this.count=count;
        this.tag=tag;
        this.notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        //此处不能用position=list_notes.size做标志
        if(list_r!=null&&position==list_r.size()){
            return foote;
        }else {
            if(list_r.get(position).getReplyUser()==0){
                return content;
            }else{
                return content_2;
            }
        }
    }

    class ListViewHolder extends RecyclerView.ViewHolder{
        int position;
        ReplyDailog reply_reply_dailog;
        TextView reply_reply,yinyong,reply_content,reply_floor;
        ReplyUserItem reply_user;
        public ListViewHolder(View itemView) {
            super(itemView);
            reply_user= (ReplyUserItem) itemView.findViewById(R.id.reply_user);
            reply_reply_dailog= (ReplyDailog) itemView.findViewById(R.id.reply_reply_dailog);
            reply_reply= (TextView) itemView.findViewById(R.id.reply_reply);
            yinyong= (TextView) itemView.findViewById(R.id.yinyong);
            reply_content= (TextView) itemView.findViewById(R.id.reply_content);
            reply_floor= (TextView) itemView.findViewById(R.id.reply_floor);
            reply_user.setReplyUserItemCall(new ReplyUserItemCall() {
                @Override
                public void onClick(View view, int flag) {
                    switch (flag){
                        case ReplyUserItemCall.HEAD_IMAGE://点击了头像

                            break;
                        case ReplyUserItemCall.DELETE://点击了屏蔽

                            UpData(list_r,0,tag);
                            break;
                    }
                }
            });
            //点击回复
            reply_reply.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    reply_reply_dailog.setVisibility(View.VISIBLE);
                    reply_reply_dailog.setText("");
                }
            });
            reply_reply_dailog.setReplyDailogCall(new ReplyDailogCall() {
                @Override
                public void onClick(View view, int flag) {
                    switch (flag){
                        case ReplyDailogCall.CANCEL://点击取消关闭对话框
                            reply_reply_dailog.setVisibility(View.GONE);
                            break;
                        case ReplyDailogCall.OK:
                            //点击确定发送消息并关闭对话框
                            String reply_info=reply_content.getText().toString()+"\n&回复"+position+"楼:"+list_r.get(position-1).getRealName()+"\n"+reply_reply_dailog.getText();
                            ReceiveActivity(noteDetailBean.getId()+"",list_r.get(position-1).getId()+"",reply_info);
                            reply_reply_dailog.setVisibility(View.GONE);
                            break;
                    }
                }
            });
        }
    }

    private void ReceiveActivity(String order_id,String reply_user,String content){
        Map<String,String> map=new HashMap<>();
        map.put("activityId",order_id);
        map.put("content",content);
        map.put("replyUser",reply_user);
        MyOkHttpUtils.getInstance(context).doPost(Constants.RECEIVE_ACTIVITY,map)
                .excute(new MyOkHttpUtils.OKCallBack() {
                    @Override
                    public void onFailure(String message) {

                    }

                    @Override
                    public void onResponse(String message) {
                        Log.i("ActivityDetail","message=receive=Adapter=="+message);
                        detailAdapterCall.Receive();
                    }
                });
    }

    class Reply_Reply extends RecyclerView.ViewHolder{
        int position;
        TextView reply_content,reply_reply_content;
        ReplyUserItem replyUserItem;
        public Reply_Reply(View itemView) {
            super(itemView);
            reply_content= (TextView) itemView.findViewById(R.id.reply_content);
            reply_reply_content= (TextView) itemView.findViewById(R.id.reply_reply_content);
            replyUserItem= (ReplyUserItem) itemView.findViewById(R.id.reply_reply_user);
            replyUserItem.setReplyUserItemCall(new ReplyUserItemCall() {
                @Override
                public void onClick(View view, int flag) {
                    switch (flag){
                        case ReplyUserItemCall.HEAD_IMAGE://点击了头像

                            break;
                        case ReplyUserItemCall.DELETE://点击了屏蔽
                            //需要服务器配合删除
                            list_r.remove(position-1);
                            UpData(list_r,0,tag);
                            break;
                    }
                }
            });
        }
    }

    class Bottom extends RecyclerView.ViewHolder{
        TextView bottom_tv;
        public Bottom(View itemView) {
            super(itemView);
            bottom_tv= (TextView) itemView.findViewById(R.id.bottom_tv);
        }
    }
}
